package com.cy.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class GatewayConfig {
    public GatewayConfig(){
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange,
                                                      Throwable throwable) {
                //String json = "{\"code\": 429,\"msg\":\"too many request\"}";

                Map<String,Object> map=new HashMap<>();
                map.put("code","429");
                map.put("msg","too many request");
                //在实际项目中把对象转json格式的字符串，有三种方法
                //第一种方式：spring自带的 -- jackson（稳定性好）
                String json = null;
                /*
                try {
                    json = new ObjectMapper().writeValueAsString(map);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    json="{\"code\": 429,\"msg\":\"too many request\"}";
                }
                 */
                //第二种方式：阿里巴巴的 -- fastjson
                /*
                json=JSON.toJSONString(map);
                 */
                //第三种方式：谷歌的 -- Gson
                json=new Gson().toJson(map);

                return ServerResponse.ok().body(Mono.just(json),String.class);
            }
        });
    }
}
